Skip to content

[实例] 消息弹窗框

1.得到组件渲染的 DOM

js
import Vue from "vue";
/**
 * 获取组件的根元素
 * 1.创建一个新的Vue实例,使用render函数将传入的组件comp和属性props渲染为虚拟DOM。
 * 2.调用$mount方法将Vue实例挂载到内存中。
 * 3.返回Vue实例的根DOM元素$el。
 * @param {*} comp 组件
 * @param {*} props 组件的属性
 */
function getComponentRootDom(comp, props) {
  const vm = new Vue({
    render: (h) => h(comp, { props }),
  });
  vm.$mount();
  return vm.$el;
}
export default getComponentRootDom;

2.弹窗函数

js
import getComponentRootDom from "./getComponentRootDom";
import Icon from "./Icon";
import styles from "./index.less";
/**
 * 弹窗函数
 * @param {String} content 显示内容
 * @param {String} type 显示类型
 * @param {Number} duration 显示时长
 * @param {HTMLElement} container 显示容器
 * @usage showMessage("提示内容")
 */
function showMessage(content, type = "info", duration = 2000, container) {
  const div = document.createElement("div");
  const iconDom = getComponentRootDom(Icon, { type });
  div.innerHTML = `<span class="${style.icon}">${iconDom.outerHTML}</span><div class="content">${content}</div>`;
  // 添加样式
  let typeClassName = styles[`message-${type}`];
  div.className = `${style.message} ${typeClassName}`;
  // 将div 加入容器中
  if (!container) {
    container = document.body;
  } else {
    // 容器的position 是否改动
    if (container.getComputedStyle(container).position === "static") {
      container.style.position = "relative";
    }
  }
  container.appendChild(div);
  // 强制渲染
  div.clientHeight; // 导致reflow
  // 添加显示动画
  div.style.opacity = 1;
  div.style.transform = "translate(-50%, -50%)";
  // 添加消失动画
  setTimeout(() => {
    div.style.opacity = 0;
    div.style.transform = "translate(-50%, -50%) translateX(-25px)";
    div.addEventListener(
      "transitionend",
      () => {
        div.remove();
      },
      { once: true }
    );
  }, duration);
}

3.样式

less
// index.less
.message {
  .self-center();
  z-index: 999;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  padding: 10px 20px;
  line-height: 1.5;
  display: flex;
  align-items: center;
  color: #fff;
  transition: all 0.3s ease;
  transform: translate(-50%, -50%) translateX(15px);
  opacity: 0;
  &-success {
    background-color: #4caf50;
  }
  &-error {
    background-color: #f44336;
  }
  &-warning {
    background-color: #ff9800;
  }
  &-info {
    background-color: #2196f3;
  }
}

Released under the MIT License.